<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-03-10
 * Time: 22:51
 */

namespace app\ops\controller;


class Area extends Base

{

    function index(){

        $list = db('area_tag')->order('id desc')->paginate(10);

        $this->assign('list', $list);

        return $this->fetch();

    }

    function delete($id){

        db('area_tag')->where(['id'=> $id])->delete();

        return success([]);

    }

    function add(){

        if(request()->post()){
            $name = request()->param('name', '');
            $parent_id = request()->param('parent_id', 0);
            $id = request()->param('id', 0);

            if(!$name){

                return fail("NAME_NOT_FOUND");
            }

            if($id){
                db('area_tag')->where(['id'=> $id])->update([
                    'name'=> $name
                ]);
            }else{
                db('area_tag')->insert(['name'=> $name, 'parent_id'=> $parent_id]);
            }

            return success([]);

        }else{

            $list = db('area_tag')->where(['parent_id'=> 0])->select();
//            dump($list);
            $this->assign('list', $list);

            return $this->fetch();

        }

    }

    function edit($id = 0){

        $data = db('area_tag')->where(['id'=> $id])->find();

        $this->assign('data', $data);

        return $this->fetch('add');
    }

}